package com.epsoft.odin7cj.flow.api.form;

import java.util.ArrayList;
import java.util.List;

import org.camunda.bpm.engine.FormService;

//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.repository.Deployment;

import org.camunda.bpm.engine.form.FormField;
import org.camunda.bpm.engine.form.StartFormData;
import org.camunda.bpm.engine.form.TaskFormData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.epsoft.odin7cj.core.dto.RetData;
import com.epsoft.odin7cj.flow.bpo.form.FormBPO;
import com.epsoft.odin7cj.flow.bpo.task.TaskBPO;
import com.epsoft.odin7cj.flow.dao.formtmpl.FormTmplDao;
import com.epsoft.odin7cj.flow.model.form.FlowForm;
import com.epsoft.odin7cj.flow.model.formtmpl.FormTmpl;

/**
 * DESC: 流程参数 <br>
 * @author 胡义振
 * @date 2020年5月20日
 */
@RestController
@RequestMapping("/api/flow/form")
public class FormApi {

	@Autowired
	private FormService formService;
	@Autowired
	private TaskBPO taskBPO;
	/**
	 * DESC: 获取任务表单
	 * @auther 胡义振
	 * @date 2020年6月8日
	 * @param taskId
	 * @return 
	 * @return
	 */
	@RequestMapping("/getTaskForm")
	public RetData<FlowForm> getTaskForm(String taskId) throws Exception{
		RetData<FlowForm> retData = new RetData<FlowForm>();
		FlowForm retFlowForm = taskBPO.getFlowFormByTaskId(taskId);
		retData.setRetBody(retFlowForm);
		return retData;
	}
	
	/**
	 * DESC: 获取任务表单
	 * @auther 胡义振
	 * @date 2020年6月8日
	 * @param taskId
	 * @return 
	 * @return
	 */
	@RequestMapping("/getTaskFormList")
	public RetData<List<FlowForm>> getTaskFormList(String taskId) {
		RetData<List<FlowForm>> retData = new RetData<List<FlowForm>>();
		List<FlowForm> taskFormList = new ArrayList<FlowForm>();
		if(taskId!=null && !taskId.equals("0")) {
			TaskFormData taskForm = formService.getTaskFormData(taskId);
			List<FormField> listFormField = taskForm.getFormFields();
			for(FormField formField : listFormField) {
				FlowForm t = new FlowForm();
				t.setId(formField.getId());
				t.setFieldName(formField.getId());
				t.setFieldValue(formField.getDefaultValue());
				t.setLabelName(formField.getLabel());
				t.setTypeName(formField.getTypeName());
				t.setProperties(formField.getProperties());
				taskFormList.add(t);
			}
		}
		retData.setRetBody(taskFormList);
		return retData;
	}
	
	/**
	 * DESC: 获取开始时间表单
	 * @param fdId 流程定义ID 
	 * @return
	 */
	@RequestMapping("/getStartFormList")
	public RetData<List<FlowForm>> getStartFormList(String fdId) {
		RetData<List<FlowForm>> retData = new RetData<List<FlowForm>>();
		List<FlowForm> startFormList = new ArrayList<FlowForm>();
		StartFormData startForm = formService.getStartFormData(fdId);
		List<FormField> listFormField = startForm.getFormFields();
		for(FormField formField : listFormField) {
			FlowForm t = new FlowForm();
			t.setId(formField.getId());
			t.setFieldName(formField.getId());
			t.setFieldValue(formField.getDefaultValue());
			t.setLabelName(formField.getLabel());
			t.setTypeName(formField.getTypeName());
			t.setProperties(formField.getProperties());
			startFormList.add(t);
		}
		retData.setRetBody(startFormList);
		return retData;
	}
	
	
}
